package com.zy.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.mapper.RoleMapper;
import com.zy.pojo.Role;
import com.zy.service.RoleService;

import com.zy.vo.PageAto;
import com.zy.vo.RoleVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<Role> getRoleList(RoleVo roleVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(roleVo.getUserName()),"username",roleVo.getUserName())
                .like(!StringUtils.isEmpty(roleVo.getBianHao()),"bianhao",roleVo.getBianHao())
//                .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
                .gt(roleVo.getStime()!=null,"birthday",roleVo.getStime())
                .lt(roleVo.getEtime()!=null,"birthday",roleVo.getEtime());

        return super.list(queryWrapper);
    }

    @Override
    public PageAto getPageRoleList(Integer pageNo, Integer pageSize, RoleVo roleVo) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(roleVo.getUserName()),"username",roleVo.getUserName())
                .like(!StringUtils.isEmpty(roleVo.getBianHao()),"bianhao",roleVo.getBianHao())
//                .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
                .gt(roleVo.getStime()!=null,"birthday",roleVo.getStime())
                .lt(roleVo.getEtime()!=null,"birthday",roleVo.getEtime());
      List<Role> list = roleMapper.getPageRoleList(page,queryWrapper);
        return new PageAto(list,page.getTotal());
    }

    @Override
    public Role getRoleUsername(String username) {
       Role role = roleMapper.getRoleUsername(username);
        return role;
    }


    @Override
    public Role getRoleId(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Integer id) {
        roleMapper.deleteById(id);
    }
}
